﻿/*
Plugin for the SingBot IRC Bot [http://singbot.unix-net.ru]
Copyright (C) 2014 adamix

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#region Using directives
using System;
using System.Collections.Generic;
using System.Xml;
using SingBot.Util;
#endregion

namespace SingBot.Plugins {
	public class Example : Plugin {

		#region " Constructor/Destructor "
		public Example(Bot bot)
			: base(bot) {
			Bot.OnChannelMessage += new IrcEventHandler(Bot_OnMessage);
			Bot.OnQueryMessage += new IrcEventHandler(Bot_OnMessage);
		}
		#endregion


		#region " Methods "

		#endregion


		#region " Event handles "
		void Bot_OnMessage(Network n, Irc.IrcEventArgs e) 
        {
            if (!IsChannelEnabled(e.Data.Channel)) return;
            string[] args = e.Data.Message.Split(' ');
            if (args.Length < 1)
            {
                return;
            }
            else
            {
                if (args[0] == "!news")
                {
                    if(args.Length != 2)
                    {
                        n.SendMessage(SingBot.Irc.SendType.Notice, e.Data.Nick, "!news main - главные новости.");
                        n.SendMessage(SingBot.Irc.SendType.Notice, e.Data.Nick, "!news hitech - новости Hi-Tech.");
                        n.SendMessage(SingBot.Irc.SendType.Notice, e.Data.Nick, "!news hw - новости о Hardware.");
                        n.SendMessage(SingBot.Irc.SendType.Notice, e.Data.Nick, "!news security - новости о безопасности.");
                        n.SendMessage(SingBot.Irc.SendType.Notice, e.Data.Nick, "!news world - новости в мире.");
                        n.SendMessage(SingBot.Irc.SendType.Notice, e.Data.Nick, "!news inet - новости о интернете.");
                        return;
                    }
                    if(args[1] == "main")
                    {
                        RssManager manager = new RssManager("http://news.yandex.ru/index.rss");
                        var feed = manager.GetFeed();
                        if (feed.Count < 5)
                            return;

                        for (int i = feed.Count-1; i > (feed.Count - 6); i-- )
                        {
                            var item = feed[i];
                            n.SendMessage(SingBot.Irc.SendType.Notice, e.Data.Nick, "Главные новости: " + item.Title + " ( " + item.Link + " )");
                        }

                    }

                    if (args[1] == "hitech")
                    {
                        RssManager manager = new RssManager("http://news.yandex.ru/computers.rss");
                        var feed = manager.GetFeed();
                        if (feed.Count < 5)
                            return;

                        for (int i = feed.Count - 1; i > (feed.Count - 6); i--)
                        {
                            var item = feed[i];
                            n.SendMessage(SingBot.Irc.SendType.Notice, e.Data.Nick, "Hi-Tech: " + item.Title + " ( " + item.Link + " )");
                        }

                    }

                    if (args[1] == "hw")
                    {
                        RssManager manager = new RssManager("http://news.yandex.ru/hardware.rss");
                        var feed = manager.GetFeed();
                        if (feed.Count < 5)
                        {
                            return;
                        }

                        for (int i = feed.Count - 1; i > (feed.Count - 6); i--)
                        {
                            var item = feed[i];
                            n.SendMessage(SingBot.Irc.SendType.Notice, e.Data.Nick, "Hardware: " + item.Title + " ( " + item.Link + " )");
                        }
                    }

                    if (args[1] == "security")
                    {
                        RssManager manager = new RssManager("http://news.yandex.ru/security.rss");
                        var feed = manager.GetFeed();
                        if (feed.Count < 5)
                        {
                            return;
                        }

                        for (int i = feed.Count - 1; i > (feed.Count - 6); i--)
                        {
                            var item = feed[i];
                            n.SendMessage(SingBot.Irc.SendType.Notice, e.Data.Nick, "Безопасность: " + item.Title + " ( " + item.Link + " )");
                        }
                    }

                    if (args[1] == "world")
                    {
                        RssManager manager = new RssManager("http://news.yandex.ru/world.rss");
                        var feed = manager.GetFeed();
                        if (feed.Count < 5)
                        {
                            return;
                        }

                        for (int i = feed.Count - 1; i > (feed.Count - 6); i--)
                        {
                            var item = feed[i];
                            n.SendMessage(SingBot.Irc.SendType.Notice, e.Data.Nick, "В мире: " + item.Title + " ( " + item.Link + " )");
                        }
                    }

                    if (args[1] == "inet")
                    {
                        RssManager manager = new RssManager("http://news.yandex.ru/internet.rss");
                        var feed = manager.GetFeed();
                        if (feed.Count < 5)
                        {
                            return;
                        }

                        for (int i = feed.Count - 1; i > (feed.Count - 6); i--)
                        {
                            var item = feed[i];
                            n.SendMessage(SingBot.Irc.SendType.Notice, e.Data.Nick, "В мире: " + item.Title + " ( " + item.Link + " )");
                        }
                    }
                }

            }
		}

		#endregion
	}
}
